13 vector
<bool> forbidden(10000, false);
14 vector
<bool> visited(10000, false);
18 for (int i
=0; i
<4; ++i
){
19 int x
; scanf("%d", &x
);
20 start
= (start
* 10) + x
;
24 for (int i
=0; i
<4; ++i
){
25 int x
; scanf("%d", &x
);
33 for (int i
=0; i
<4; ++i
){
34 int x
; scanf("%d", &x
);
40 queue
<pair
<int, int> > q
;
41 //El primer numero es el nodo que voy a visitar, el segundo es la minima distancia en que lo puedo visitar
43 q
.push(make_pair(start
, 0));
45 int node
= q
.front().first
;
46 int dist
= q
.front().second
;
56 for (int i
=1; i
<=4; ++i
){
57 int pow10
= 1; for (int j
=1; j
<=i
; ++j
) pow10
*= 10;
58 int digit
= (node
% pow10
)* 10/pow10
;
60 int v
= node
- digit
* pow10
/10;
62 int x
= v
+ pow10
/10 * ((digit
+1)%10);
63 int y
= v
+ pow10
/10 * ((digit
+9)%10);
65 if (!forbidden
[x
] && !visited
[x
]){
66 q
.push(make_pair(x
, dist
+ 1));
68 if (!forbidden
[y
] && !visited
[y
]){
69 q
.push(make_pair(y
, dist
+ 1));
75 cout
<< answer
<< endl
;